package com.niuxiaofa.newlaw.Network;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.niuxiaofa.newlaw.Bean.AreaCodeBean;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Bean.UserInfoBean;
import com.niuxiaofa.newlaw.Bean.UserLoginBean;
import com.niuxiaofa.newlaw.Tools.UIUtils;

import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



public class UserPresenter {

    /**
     *
     */
    public static void login(Map map, interface_login interface_login) {


        Gson gson = new Gson();

        String json=gson.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);

        Call<UserLoginBean> call = ServiceManager.getInstance().creat(ApiService.class).login(body);
        call.enqueue(new Callback<UserLoginBean>() {
            @Override
            public void onResponse(Call<UserLoginBean> call, Response<UserLoginBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    UserLoginBean userLogin = (UserLoginBean) response.body();
                    interface_login.getResponse(userLogin);
                }
            }

            @Override
            public void onFailure(Call<UserLoginBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_login {
        void getResponse(UserLoginBean u);
    }


        public static void smsCode(Map map) {

            Gson gson = new Gson();

            String json=gson.toJson(map);

            RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);


            Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).smsCode(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    BaseBean baseBean = (BaseBean) response.body();
                    Toast.makeText(UIUtils.getContext(),baseBean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }



    public static void checkCode(Map map) {

        Gson gson = new Gson();

        String json=gson.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);


        Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).checkCode(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    BaseBean baseBean = (BaseBean) response.body();
                    Toast.makeText(UIUtils.getContext(),baseBean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }





    public static void getAreaCode( interface_getAreaCode interface_getAreaCode) {

        Call<AreaCodeBean> call = ServiceManager.getInstance().creat(ApiService.class).getAreaCode();

        call.enqueue(new Callback<AreaCodeBean>() {
            @Override
            public void onResponse(Call<AreaCodeBean> call, Response<AreaCodeBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    AreaCodeBean userLogin = (AreaCodeBean) response.body();
                    interface_getAreaCode.getResponse(userLogin);
                }
            }

            @Override
            public void onFailure(Call<AreaCodeBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_getAreaCode {
        void getResponse(AreaCodeBean u);
    }




    public static void userInfo(interface_userInfo interface_userInfo) {

        Call<UserInfoBean> call = ServiceManager.getInstance().creat(ApiService.class).userInfo();

        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    UserInfoBean baseBean = (UserInfoBean) response.body();
                    interface_userInfo.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {
            Log.e("链接错误", call.request().url()+" "+t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_userInfo {
        void getResponse(UserInfoBean u);
    }


}
